
import { _decorator, Node, Sprite, SpriteFrame } from 'cc';
import { RenderManager } from '../Base/RenderManager';
import { Item_Status_Enum, Item_Type_Enum } from '../Enum';
import DataManager from '../Runtime/DataManager';
const { ccclass, property } = _decorator;


@ccclass('ItemManager')
export class ItemManager extends RenderManager {
    label: string = "物品";
    type: Item_Type_Enum;

    @property(SpriteFrame)
    sceneSp: SpriteFrame = null;
    @property(SpriteFrame)
    inventorySp: SpriteFrame = null;

    start() {
        super.start();
        this.node.on(Node.EventType.TOUCH_END, this.touchEnd, this);
    }

    protected onDestroy(): void {
        super.onDestroy();
        this.node.off(Node.EventType.TOUCH_END, this.touchEnd, this);
    }

    render() {
        const status = DataManager.Instance.items.find(i => i.type === this.type)?.status;
        const spComp: Sprite = this.node.getComponent(Sprite);
        if (!status) {
            return
        }
        switch (status) {
            case Item_Status_Enum.Scene:
                this.node.active = true;
                spComp.spriteFrame = this.sceneSp;
                break;
            case Item_Status_Enum.Inventory:
                this.node.active = true;
                spComp.spriteFrame = this.inventorySp;
                break;
            case Item_Status_Enum.Disabled:
                this.node.active = false;
                break;
            default:
                break;
        }
    }

    touchEnd(event) {
        const item = DataManager.Instance.items.find(i => i.type === this.type);
        if (!item) {
            return;
        }
        if (item.status === Item_Status_Enum.Scene) {
            item.status = Item_Status_Enum.Inventory;
            DataManager.Instance.items = [...DataManager.Instance.items]
        }
    }
}
